package com.zhou.util;


import com.zhou.framework.exception.VerifyException;

import java.io.File;
import java.util.Collection;
import java.util.Map;
import java.util.Objects;

/**
 * @author lang.zhou
 * @date 2020/1/3
 */
public class Assert {
    public static void notNull(Object object) {
        notNull(object,"不合法请求！");
    }
    public static void notNull(Object object, String message, Object... args) {
        if(object == null) {
            throw new VerifyException(String.format(message,args));
        }
    }
    public static void isNull(Object object, String message,Object... args) {
        if(object != null) {
            throw new VerifyException(String.format(message,args));
        }
    }
    public static void equals(Object a, Object b ,String message,Object... args) {
        if(!Objects.equals(a,b)) {
            throw new VerifyException(String.format(message,args));
        }
    }


    public static void notEmpty(Collection<?> c, String message,Object... args) {
        notNull(c, message,args);
        if(c.isEmpty()) {
            throw new VerifyException(String.format(message,args));
        }
    }

    public static void notEmpty(Map<?,?> c, String message,Object... args) {
        notNull(c, message,args);
        if(c.isEmpty()) {
            throw new VerifyException(String.format(message,args));
        }
    }
    public static void notEmpty(String c) {
        notEmpty(c,"不合法请求！");
    }
    public static void notEmpty(String c, String message,Object... args) {
        notNull(c, message,args);
        if(c.trim().length() == 0) {
            throw new VerifyException(String.format(message,args));
        }
    }
    public static void maxLength(String c , int max, String message,Object... args) {
        if(c != null){
            int len = StringTool.getStringLength(c,1);
            isTrue(len <= max,String.format(message,args));
        }
    }



    public static void error(String msg) {
        throw new VerifyException(msg);
    }
    public static void isTrue(boolean cond, String message,Object... args) {
        if(!cond) {
            throw new VerifyException(String.format(message,args));
        }
    }

    public static void below0(Number n, String message,Object... args) {
        notNull(n, message,args);
        if(n instanceof Integer){
            if(n.intValue() <= 0) {
                throw new VerifyException(String.format(message,args));
            }
        }else{
            if(n.floatValue() <= 0f) {
                throw new VerifyException(String.format(message,args));
            }
        }

    }
    public static void fileExist(File file, String message,Object... args) {
        if(file == null || !file.exists()) {
            throw new VerifyException(String.format(message,args));
        }
    }
}
